﻿#region   注释
/************************************************************************************
 * 
 *日期：2008-10-27
 *作者：张华
 *描述： *用户评论信息数据访问类
 * 
 * 
 * 
 *************************************************************************************/
#endregion

#region   命名空间
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using Model;
using SiteCommon;
#endregion

namespace SQLServerDAL
{
    public class UserCommentDA
    {

        #region 检验用户是否购买此商品
        /// <summary>
        /// 检验用户是否购买此商品
        /// </summary>
        /// <param name="userId">用户ID</param>
        /// <param name="goodsId">商品ID</param>
        /// <returns>True:购买过 False:未购买过</returns>/returns>
        public bool CheckUserOrderThisGoods(string userId, string goodsId)
        {
            string sql = " SELECT  1  FROM ORDERDOC  AS I " +
                           " INNER JOIN ORDERDETAIL AS G  ON I.order_id=G.OD_id " +
                           " WHERE I.ORDER_CUSTOMERCODE='" + userId + " 'AND G.OD_CMDCODE='" + goodsId + "'";

            DataSet ds;
            string ret = SqlManager.MySqlHelper.ExecDatasetSql(sql, out ds);
            if (ret.Equals("000000"))
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    return true;
                }
            }

            return false;
        }
        #endregion

        #region   发布评论
        /// <summary>
        /// 发布评论(前台)
        /// </summary>
        /// <param name="userComment">传入一个COMMODITY_EVAL类型的参数</param>
        /// <returns>000000为成功,其他为失败信息</returns>
        public string Add(Model.COMMODITY_EVAL Eval)
        {
            int rows;
            SqlParameter outParam = new SqlParameter("@ReturnValue", SqlDbType.NVarChar, 10);
            outParam.Direction = ParameterDirection.Output;
            SqlParameter[] param = new SqlParameter[]
            {
              new SqlParameter("@UserId",Eval.CE_USER),
              new SqlParameter("@GoodsId",Eval.CE_CODE),
              new SqlParameter("@Rank",Eval.CE_LEVEL),
              new SqlParameter("@UserIP",Eval.CE_FIELD20),
              new SqlParameter("@Title",Eval.CE_FIELD1),
              new SqlParameter("@CommentContent",Eval.CE_EVAL),
              outParam
            };
            string ret = SqlManager.MySqlHelper.ExecNonSP("UserCommentAdd", param, out rows);
            if (ret.Equals("000000"))
            {
                return outParam.Value.ToString();
            }
            return ret;

        }
        #endregion

        #region  获取评论信息列表
        /// <summary>
        ///  获取该商品评论信息


        /// </summary>
        /// <param name="pageIndex">当前页面</param>
        /// <param name="pageSize">每页显示多少</param>
        /// <param name="goodsId">商品id</param>
        /// <param name="recordCount">总共多少条记录</param>
        /// <param name="commentList">返回评论类型的List</param>
        /// <returns>'000000'为成功其他为失败</returns>
        public string GetUserCommentByGoodsId(int pageIndex,
            int pageSize,
            string goodsId,
            out int recordCount,
            out List<Model.COMMODITY_EVAL> commentList)
        {
            commentList = null;
            recordCount = 0;
            string tableName = "COMMODITY_EVAL  AS C";
            string fields = "C.*";
            string condition = " and C.Show=1";
            if (goodsId != "")
            {
                condition += " and C.CE_CODE= '" + goodsId + "'";
            }
            string keyField = "C.CE_ID";
            string sort = "DESC";
            DataTable dt;
            string ret = SqlManager.MySqlHelper.GetPagingRecordIndex(
            pageIndex,
            pageSize,
            tableName,
            fields,
            condition,
            keyField,
            sort,
            out recordCount,
            out dt);
            if (ret.Equals("000000"))
            {
                commentList = new List<Model.COMMODITY_EVAL>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    Model.COMMODITY_EVAL userComment = new Model.COMMODITY_EVAL();
                    userComment.CE_ID = Common.GetInt(dr["CE_ID"]);
                    userComment.CE_LEVEL = Common.GetString(dr["CE_LEVEL"]);
                    userComment.CE_FIELD20 = Common.GetString(dr["CE_FIELD20"]);
                    userComment.CE_FIELD1 = Common.GetString(dr["CE_FIELD1"]);
                    userComment.CE_EVAL = Common.GetString(dr["CE_EVAL"]);
                    userComment.CE_TIME = Common.GetDateTime(dr["CE_TIME"]);
                    userComment.CE_USER = dr["CE_USER"].ToString();
                    commentList.Add(userComment);
                }
            }
            return ret;
        }
        #endregion

        #region  获取单个会员评论信息列表
        /// <summary>
        ///  获取该用户在该商品类型下的评论信息


        /// </summary>
        /// <param name="pageIndex">当前页面</param>
        /// <param name="pageSize">每页显示多少</param>
        /// <param name="userId">用户ID</param>
        /// <param name="typeId">商品类型Id</param>
        /// <param name="recordCount">总共多少条记录</param>
        /// <param name="commentList">返回评论类型的List</param>
        /// <returns>'000000'为成功其他为失败</returns>
        public string GetUserCommentByUserIdAndTypeId(int pageIndex,
            int pageSize,
            string userId,
            out int recordCount,
            out List<Model.COMMODITY_EVAL> commentList)
        {
            commentList = null;
            recordCount = 0;
            string tableName = "COMMODITY_EVAL  AS C inner join  COMMODITY_AUTO AS G on C.CE_CODE=G.CA_CODE";
            string fields = "C.*,G.CA_NAME,G.CA_CODE";
            string condition = "";
            if (userId != "")
            {
                condition += " and C.CE_User= '" + userId + "'";
            }
            string keyField = "C.CE_ID";
            string sort = "DESC";
            DataTable dt;
            string ret = SqlManager.MySqlHelper.GetPagingRecordIndex(
            pageIndex,
            pageSize,
            tableName,
            fields,
            condition,
            keyField,
            sort,
            out recordCount,
            out dt);
            if (ret.Equals("000000"))
            {
                commentList = new List<Model.COMMODITY_EVAL>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    Model.COMMODITY_EVAL userComment = new Model.COMMODITY_EVAL();
                    userComment.CE_ID = Common.GetInt(dr["CE_ID"]);
                    userComment.CE_LEVEL = Common.GetString(dr["CE_LEVEL"]);
                    userComment.CE_TIME = Common.GetDateTime(dr["CE_TIME"]);
                    userComment.CE_EVAL = Common.GetString(dr["CE_EVAL"]);
                    userComment.CE_FIELD1 = Common.GetString(dr["CE_FIELD1"]);
                    userComment.CE_CODE = Common.GetString(dr["CA_NAME"]);
                    userComment.CE_FIELD20 = Common.GetString(dr["CE_FIELD20"]);
                    userComment.CE_USER = Common.GetString(dr["CA_CODE"]);

                    commentList.Add(userComment);
                }
            }
            return ret;
        }
        #endregion

        #region   获取所有用户留言列表
        /// <summary>
        /// 获取所有用户留言列表
        /// </summary>
        /// <param name="pageIndex">当前第几页</param>
        /// <param name="pageSize">每页显示多少条</param>
        /// <param name="recordCount">共多少记录</param>
        /// <param name="messageList">返回一个list列表</param>
        /// <returns>000000为成功,其他信息则为失败</returns>
        public string GetAllComment(int pageIndex, int pageSize, out int recordCount, out List<Model.COMMODITY_EVAL> commentList)
        {
            commentList = null;
            recordCount = 0;
            string tableName = "COMMODITY_EVAL INNER JOIN COMMODITY_AUTO ON CA_CODE=CE_CODE INNER JOIN EVAL_LEVEL ON CE_LEVEL=EL_CODE ";
            string fields = "CE_USER,CE_ID,CE_CODE,CA_NAME,CE_FIELD1,CE_EVAL,EL_NAME,AddJF,Show ";
            string condition = "";
            string keyfield = "CE_ID";
            string sort = "DESC";
            DataTable dt;
            string ret = SqlManager.MySqlHelper.GetPagingRecordIndex(
            pageIndex,
            pageSize,
            tableName,
            fields,
            condition,
            keyfield,
            sort,
            out recordCount,
            out dt);
            if (ret.Equals("000000"))
            {
                commentList = new List<Model.COMMODITY_EVAL>(dt.Rows.Count);
                foreach (DataRow dr in dt.Rows)
                {
                    Model.COMMODITY_EVAL UComment = new Model.COMMODITY_EVAL();
                    UComment.CE_USER = Common.GetString(dr["CE_USER"]);
                    UComment.CE_ID = Common.GetInt(dr["CE_ID"]);
                    UComment.CE_CODE = Common.GetString(dr["CE_CODE"]);//商品代码
                    UComment.CE_FIELD1 = Common.GetString(dr["CE_FIELD1"]);// 评价标题
                    UComment.CE_FIELD20 = Common.GetString(dr["CA_NAME"]);//商品名称
                    UComment.CE_EVAL = Common.GetString(dr["CE_EVAL"]);//评价内容
                    UComment.CE_LEVEL = Common.GetString(dr["EL_NAME"]);//评价等级名称
                    UComment.AddJF=Common.GetDecimal(dr["AddJF"]);
                    UComment.Show = Common.GetInt(dr["Show"]);
                    commentList.Add(UComment);
                }

            }
            return ret;
        }
        #endregion

        
        #region   根据留言ID更改评价积分和显示
        /// <summary>
        /// 根据留言Id更改评价积分和显示
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="backContent"></param>
        /// <returns></returns>
        public string UserCommentEdit(int commentId, decimal addjf, int show)
        {
            string sql = "Update COMMODITY_EVAL Set BYZD1=1,AddJF=" + addjf + ", Show=" + show + " Where CE_ID=" + commentId;
            return sql;

        }
        #endregion

        #region   根据留言Id获取评价信息
        /// <summary>
        ///  根据留言Id获取评价信息
        /// </summary>
        /// <param name="messageId"></param>
        /// <param name="myUserMessage"></param>
        /// <returns></returns>
        public string getUserCommentById(int commentId, out Model.COMMODITY_EVAL myUserComment)
        {
            myUserComment = new Model.COMMODITY_EVAL();
            DataSet ds;
            string sql = "select * from  COMMODITY_EVAL where CE_ID=" + SiteCommon.Common.GetInt(commentId);
            string ret = SqlManager.MySqlHelper.ExecDatasetSql(sql, out ds);
            if (ret.Equals("000000"))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    myUserComment.CE_TIME = SiteCommon.Common.GetDateTime(dr["CE_TIME"]);
                    myUserComment.CE_CODE = SiteCommon.Common.GetString(dr["CE_CODE"]);
                    myUserComment.CE_EVAL = SiteCommon.Common.GetString(dr["CE_EVAL"]);
                    myUserComment.CE_ID = SiteCommon.Common.GetInt(dr["CE_ID"]);
                    myUserComment.CE_FIELD1 = SiteCommon.Common.GetString(dr["CE_FIELD1"]);
                    myUserComment.CE_USER = SiteCommon.Common.GetString(dr["CE_USER"]);
                    myUserComment.CE_FIELD20 = SiteCommon.Common.GetString(dr["CE_FIELD20"]);
                    myUserComment.AddJF = SiteCommon.Common.GetInt(dr["AddJF"]);
                    myUserComment.CE_LEVEL = SiteCommon.Common.GetString(dr["CE_LEVEL"]);
                    myUserComment.Show = SiteCommon.Common.GetInt(dr["Show"]);
                    myUserComment.BYZD1=SiteCommon.Common.GetInt(dr["BYZD1"]);
                }
            }
            return ret;

        }
        #endregion
    }
}
